/**
 * Created by guest on 16/6/23.
 */

//服务器数据加载
function tagsInit(){
    var tagsFormServer =  '[{"id":"1","name":"javascript"},{"id":"2","name":"java"}]';//todo 替换掉
    $("#tagDataList").attr("data",tagsFormServer);
}
tagsInit();
var tagDataList = $("#tagDataList");
var tagsObjFormServer = JSON.parse(tagDataList.attr("data"));
var indexArray = new Array();
for(index in tagsObjFormServer){
    tag = tagsObjFormServer[index];
    indexArray[tagsObjFormServer[index].name] = tagsObjFormServer[index];


}

function tagDataListAddTag(tag){
    var buttonNode = document.createElement("a");
    buttonNode.setAttribute("data-id",tag.id);
    buttonNode.setAttribute("data",JSON.stringify(tag));
    buttonNode.setAttribute("href","javascript:0;");
    buttonNode.setAttribute("class","data-li");
    buttonNode.innerHTML = tag.name;
    //定义单机事件
    buttonNode.onclick = function(){
        $("#choiced_tag").append($(this));
        //修改样式
        $(this).css("display","inline-block");
        $(this).css("background-color","#2d578d");
        $(this).css("color","white");
        $(this).css("border-radius","3px");
        $(this).css("margin","0 3px");
        // 修改单击相应:单击即删除该对象
        this.onclick = function(){
            $(this).remove();
        };

    };

    tagDataList.append(buttonNode);
    var left = $("#tag_input").offset().left;
    tagDataList.css("left",left);
    tagDataList.css("display","block");
}
function clearTagDataList(){
    tagDataList.html("");
}


function showAddTagButton(){
    $("#addTagButton").css("display","block");
    //调整位置
    var left = $("#tag_input").offset().left;
    $("#addTagButton").css("left",left);

}

var showAddTagWin =  function(){
    $("#hideWindow").css("display","block");
};

$("#addTagButton a").on("click",function(){
    $("#hideWindow").css("display","block");
});

$("#tag_input").on("keydown",function(e){
//
    if(e.keyCode == 13){
        clearTagDataList();
        var inputStr = $("#tag_input").val();
        for(key in indexArray){
            if(inputStr.trim().length <= 0){
                tagDataListAddTag(indexArray[key]);
            } else {

                //找到匹配的key
                if(key.indexOf(inputStr) != -1){
                    tagDataListAddTag(indexArray[key]);
                } else {//没找到弹出添加界面
                    showAddTagButton();
                }

            }


        }
    }
});

//
$("#win_close").on("click",function(){
    $("#hideWindow").css("display","none");
});


//album 交互js

//获取数据
var albumData = '[{"id":"1","code":"alpha","desc":"编程基础"},{"id":"2","code":"server","desc":"服务端开发"},{"id":"3","code":"client","desc":"前端开发"},{"id":"4","code":"db","desc":"数据库"},{"id":"5","code":"env","desc":"编程环境"},{"id":"6","code":"album","desc":"专题系列"},{"id":"7","code":"bugnote","desc":"错误笔记"},{"id":"8","code":"monkey","desc":"程序员"},{"id":"9","code":"news","desc":"业界资讯"},{"id":"10","code":"doc","desc":"资料下载"},{"id":"11","code":"mark","desc":"推荐"}]';
var albumDataArray =  $.parseJSON(albumData);
var albumKeyMap = new Array();
for(id  in albumDataArray){
    var obj = albumDataArray[id];
    albumKeyMap[obj.desc] = obj;
}

function clearAlbumListData(){
    $("#albumList").html("");
}
function putAlbumData(album){
    //设置下拉列表可见
    $("#albumList").css("display","block");

    var aNode = document.createElement("a");
    aNode.setAttribute("data-id",album.id);
    aNode.setAttribute("data",JSON.stringify(album));
    aNode.setAttribute("class","data-li");
    aNode.setAttribute("href","javascript:0;");
    aNode.innerHTML = album.desc;
    //设置位置


    //定义单机事件
    aNode.onclick = function(){
        $("#chooseAlbums").append($(this));
        //改变样式
        $(this).css("display","inline-block");
        $(this).css("background-color","#2d578d");
        $(this).css("color","white");
        $(this).css("border-radius","3px");
        $(this).css("margin","0 3px");
        // 修改单击相应:单击即删除该对象
        this.onclick = function(){
            $(this).remove();
        };

    };
    //设置albumList位置
    var left = $("#choice_albums").offset().left;
    $("#albumList").css("left",left);
    $("#albumList").append(aNode);
}

//点击空白处隐藏弹出菜单
$(document).on("click",function(){
    $("#albumList").css("display","none");
    tagDataList.css("display","none");
    $("#addTagButton").css("display","none");
});
$("#albumList").on("click",function(){
    return false;
//        $("#albumList").css("display","none");
});
tagDataList.on("click",function(){
    return false;
//        $("#albumList").css("display","none");
});
$("#addTagButton").on("click",function(){
    return false;
//        $("#albumList").css("display","none");
});




$("#choice_albums").on("keydown",function(e){
//
    if(e.keyCode == 13){
        clearAlbumListData();
        var inputStr = $("#choice_albums").val();
        for(key in albumKeyMap){
            //找到匹配的key
            if(inputStr.trim().length <= 0){
                putAlbumData(albumKeyMap[key]);
                continue;
            }
            if(key.indexOf(inputStr) != -1 || albumKeyMap[key].code.indexOf(inputStr) != -1){
                //列出值来
                putAlbumData(albumKeyMap[key]);
            }
        }
    }
});

var closeAddTagWin = function(){
    $("#hideWindow").css("display","none");
};
$("#tag_add_cancel").on("click",closeAddTagWin);
$("#closeAddTagWin").on("click",closeAddTagWin);

var openUploadImgWin = function(){
    $("#uploadImg").css("display","block");
}
var closeUploadImgWin = function(){
    $("#uploadImg").css("display","none");
}
$("#openImgUpWin").on("click",openUploadImgWin);
$("#close_up_img_win").on("click",closeUploadImgWin);
$("#cancel_upload").on("click",closeUploadImgWin);






